{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# for"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 遍历效率比较"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.32 ms ± 48.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "def test1():\n",
    "    t=0;l=range(10000);\n",
    "    for i in l:\n",
    "        out=l[t];t+=1\n",
    "def test2():\n",
    "    g=(i for i in range(10000))\n",
    "    t=0;l=range(10000);\n",
    "    for i in l:\n",
    "        out=next(g)\n",
    "\n",
    "def test3():\n",
    "    for i,j in zip(range(10000),range(10000)):\n",
    "        out=j\n",
    "import numpy as np \n",
    "def test4():\n",
    "    l=np.array(range(10000))\n",
    "    l=l.reshape(100,100)\n",
    "    for i in range(100):\n",
    "        for j in range(100):\n",
    "            out=l[i][j]\n",
    "#%timeit test4()\n",
    "# %timeit test3()\n",
    "# %timeit test1()\n",
    "%timeit test2()\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.2 ms ± 248 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n",
    "# 478 µs ± 26.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n",
    "# 1.58 ms ± 40.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n",
    "# 1.28 ms ± 17.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 创建生成器 注意 enumerate 对象和生成器的返回值在 *好运算上的不同 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<enumerate object at 0x000001DBC16D5900>\n",
      "(0, 2, 0, 2, 0, 2, 0, 2)\n",
      "<generator object <genexpr> at 0x000001DBC1642C50>\n",
      "2\n",
      "34\n",
      "5\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "a=[2,34,5,5]\n",
    "golist=enumerate(a)\n",
    "print(golist)\n",
    "print(next(golist)*4)\n",
    "golist=(i for i in a)\n",
    "print(golist)\n",
    "for i in golist:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[91, 92, 93, 94, 95, 96, 97, 98, 99]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ex for in  if \n",
    "N=[]\n",
    "for i in range(100):\n",
    "    nat.append(i)\n",
    "[x for x in range(100) if x>90]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0,\n",
       " 1,\n",
       " 2,\n",
       " 3,\n",
       " 4,\n",
       " 5,\n",
       " 6,\n",
       " 7,\n",
       " 8,\n",
       " 9,\n",
       " 10,\n",
       " 11,\n",
       " 12,\n",
       " 13,\n",
       " 14,\n",
       " 15,\n",
       " 16,\n",
       " 17,\n",
       " 18,\n",
       " 19,\n",
       " 20,\n",
       " 21,\n",
       " 22,\n",
       " 23,\n",
       " 24,\n",
       " 25,\n",
       " 26,\n",
       " 27,\n",
       " 28,\n",
       " 29,\n",
       " 30,\n",
       " 31,\n",
       " 32,\n",
       " 33,\n",
       " 34,\n",
       " 35,\n",
       " 36,\n",
       " 37,\n",
       " 38,\n",
       " 39,\n",
       " 40,\n",
       " 41,\n",
       " 42,\n",
       " 43,\n",
       " 44,\n",
       " 45,\n",
       " 46,\n",
       " 47,\n",
       " 48,\n",
       " 49,\n",
       " 50,\n",
       " 51,\n",
       " 52,\n",
       " 53,\n",
       " 54,\n",
       " 55,\n",
       " 56,\n",
       " 57,\n",
       " 58,\n",
       " 59,\n",
       " 60,\n",
       " 61,\n",
       " 62,\n",
       " 63,\n",
       " 64,\n",
       " 65,\n",
       " 66,\n",
       " 67,\n",
       " 68,\n",
       " 69,\n",
       " 70,\n",
       " 71,\n",
       " 72,\n",
       " 73,\n",
       " 74,\n",
       " 75,\n",
       " 76,\n",
       " 77,\n",
       " 78,\n",
       " 79,\n",
       " 80,\n",
       " 81,\n",
       " 82,\n",
       " 83,\n",
       " 84,\n",
       " 85,\n",
       " 86,\n",
       " 87,\n",
       " 88,\n",
       " 89,\n",
       " 90,\n",
       " 91,\n",
       " 92,\n",
       " 93,\n",
       " 94,\n",
       " 95,\n",
       " 96,\n",
       " 97,\n",
       " 98,\n",
       " 99]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[x for x in N ] # 生成数组\n",
    "#（x for x in N） # 这是一个生成器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "#  lambda in :out\n",
    "add1=lambda x:x+10\n",
    "# 操作返回值   for    条件\n",
    "[add1(x) for x in range(100) if x>80] \n",
    "[4 for x in range(100) if x>99] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'x': 99}"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a={}\n",
    "def add(x):\n",
    "    a[\"x\"]=x\n",
    "    return a\n",
    "[add(x) for x in nat  ]\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4, 4]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "map=[{\"a\":1,\"b\":2},{\"a\":3,\"b\":2}]\n",
    "[4 for j in map if j[\"a\"]==1 ]\n",
    "[4 for j in map if j[\"a\"]==1 or j[\"a\"]==3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "c\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# if 的 链式\n",
    "def add():\n",
    "    return 0\n",
    "c=9\n",
    "print(\"a\") if c>10 else print(\"c\") \n",
    "t=  1 if c>10 else 5\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# for if\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " i not in range\n",
      "i=99)\n",
      "none in []\n"
     ]
    }
   ],
   "source": [
    "# for else语句f  在for 循环执行结束 后执行\n",
    "for i in range (100):\n",
    "    if i>102:break\n",
    "else:\n",
    "    print(\" i not in range\")\n",
    "for i in range (100):\n",
    "    if i==99:\n",
    "        print (\"i=99)\");break\n",
    "else:\n",
    "    print(\" i not in range\")    \n",
    "for i in []:\n",
    "    print(i)\n",
    "else:\n",
    "    print(\"none in []\") # for 没有执行次数也会到else"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'a': 1, 'b': 2, 'c': 99}, {'a': 3, 'b': 2, 'c': 99}]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字典 列表加 字典元素\n",
    "map=[{\"a\":1,\"b\":2},{\"a\":3,\"b\":2}]\n",
    "for i in map:\n",
    "    i[\"c\"]=99\n",
    "map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]\n",
      "[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]\n",
      "(0, 10)\n",
      "(1, 11)\n",
      "(2, 12)\n",
      "(3, 13)\n",
      "(4, 14)\n",
      "(5, 15)\n",
      "(6, 16)\n",
      "(7, 17)\n",
      "(8, 18)\n",
      "(9, 19)\n",
      "(10, 20)\n",
      "(11, 21)\n",
      "0 10\n",
      "1 11\n",
      "2 12\n",
      "3 13\n",
      "4 14\n",
      "5 15\n",
      "6 16\n",
      "7 17\n",
      "8 18\n",
      "9 19\n",
      "10 20\n",
      "11 21\n"
     ]
    }
   ],
   "source": [
    "# 2元for\n",
    "N=[ i for i in range(15)];N2=[ i for i in range(10,22)]\n",
    "\n",
    "for i in N,N2:\n",
    "    print(i)\n",
    "for i in zip(N,N2): # 遍历其中最小的\n",
    "    print(i) #i=(n,n2)\n",
    "for i,j in zip(N,N2):\n",
    "    print(i,j)# i=n,j=n2\n",
    "# for i ,j in N,N2: 错误的\n",
    "#     print(i,j) # "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# generator  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# next（） send（ something）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "import uuid\n",
    "def guuid():\n",
    "    yield uuid.uuid1()\n",
    "def guuid2():\n",
    "    for i in range(3):\n",
    "        \n",
    "        yield uuid.uuid1()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "829dd2e2-b348-11e8-a1b8-144f8a368b99\n"
     ]
    }
   ],
   "source": [
    "for i in guuid(): #  for 可以迭代 guuid\n",
    "    print(i)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, '2')\n",
      "(1, 333)\n",
      "(2, 44)\n",
      "(3, 24)\n",
      "(0, {3: 3, 5: 4})\n",
      "(1, {2: 4, 4: 3})\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "'>' not supported between instances of 'str' and 'int'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-11-42ddb8b56a72>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0menumerate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m{\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m{\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[1;32mif\u001b[0m \u001b[1;34mu\"0.92\"\u001b[0m\u001b[1;33m>\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      6\u001b[0m     \u001b[1;32mTrue\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: '>' not supported between instances of 'str' and 'int'"
     ]
    }
   ],
   "source": [
    "for i in  enumerate({\"2\":23,333:44,44:22,24:[245,445]}):\n",
    "    print(i)\n",
    "for i in enumerate([{3:3,5:4},{2:4,4:3}]):\n",
    "    print(i)\n",
    "if u\"0.92\">2:\n",
    "    True"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
